package com.HospitalOutpatient.controller;

import com.HospitalOutpatient.annotation.LoginToken;
import com.HospitalOutpatient.dao.ReceptionDao;
import com.HospitalOutpatient.entity.Doctor;
import com.HospitalOutpatient.entity.Patient;
import com.HospitalOutpatient.entity.Reception;
import com.HospitalOutpatient.entity.User;
import com.HospitalOutpatient.service.ReceptionService;
import com.HospitalOutpatient.vo.PatientAndReceptionVo;
import com.HospitalOutpatient.vo.ReceptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reception")
public class ReceptionController {

    @Autowired
    private ReceptionService receptionService;

    @LoginToken
    @PostMapping("/addReception")
    public Result addReception(@RequestBody Reception reception){
        if(receptionService.addReception(reception) == false){
            return new Result(Code.SAVE_ERR,null,"挂号失败");
        }
        return new Result(Code.SAVE_OK,null,"挂号成功");
    }
    @LoginToken
    @PostMapping("/getReceptionByUserId")
    public Result getReceptionByUserId(@RequestBody User user){
        user = UserContext.get();
        UserContext.remove();
        List<ReceptionVo> receptionVoList = receptionService.getReceptionByUserId(user.getUserId());

        return new Result(Code.GET_OK,receptionVoList,"获取挂号记录成功");
    }
    @LoginToken
    @PostMapping("/getTodayReceptionByDoctorId")
    public Result getTodayReceptionByDoctorId(@RequestBody Patient patient){
        Doctor doctor = DoctorContext.get();
        DoctorContext.remove();
        List<PatientAndReceptionVo> todayReceptionList = receptionService.getTodayReception(doctor.getDoctorId(), patient.getPatientName());

        return new Result(Code.GET_OK,todayReceptionList,"获取今日待接诊单成功");
    }

    @LoginToken
    @GetMapping("/getNewReceptionByUserId")
    public Result getNewReceptionByUserId(){
        User user = UserContext.get();
        UserContext.remove();
        List<ReceptionVo> receptionVoList = receptionService.getNewReceptionByUserId(user.getUserId());
        return new Result(Code.GET_OK,receptionVoList,"获取今日待接诊单成功");
    }

    @LoginToken
    @PostMapping("/deleteReceptionById")
    public Result deleteReceptionById(@RequestBody Reception reception){
        if(receptionService.deleteReceptionById(reception.getReceptionId()) == false){
            return new Result(Code.DELETE_ERR,null,"请提前一天取消预约");
        }
        return new Result(Code.DELETE_OK,null,"取消预约成功");
    }


    @LoginToken
    @PostMapping("/getFinishReceptionByDoctorId")
    public Result getFinishReceptionByDoctorId(@RequestBody Patient patient){
        Doctor doctor = DoctorContext.get();
        DoctorContext.remove();
        List<PatientAndReceptionVo> todayReceptionList = receptionService.getFinishReception(doctor.getDoctorId(), patient.getPatientName());

        return new Result(Code.GET_OK,todayReceptionList,"获取已完成接诊单成功");
    }

}
